package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper mapper;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private IBrandRedisRepository brandRedisRepository;

    @Autowired
    private IBrandRedisRepository repository;

    @Override
    public void addNew(BrandAddNewDTO addNewDTO) {
        int count =  mapper.countByName(addNewDTO.getName());
        if (count > 0){
            String message = "添加品牌失败，名字已被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        BeanUtils.copyProperties(addNewDTO,brand);
        int insertRows = mapper.insert(brand);
        if (insertRows!=1){
            String message = "添加品牌失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        BrandStandardVO standardVO =  mapper.getStandardById(id);
        if (standardVO==null){
            String message = "删除品牌失败，该品牌不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }else if (brandCategoryMapper.countByBrand(id)>0){
            String message = "删除品牌失败，该品牌关联了某些类别！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }else if (spuMapper.countByBrand(id)>0){
            String message = "删除品牌失败，该品牌关联了某些spu！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int deleteRows = mapper.deleteById(id);
        if (deleteRows!=1){
            String message = "删除品牌失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void update(Long id, BrandUpdateDTO updateDTO) {
        BrandStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "更新品牌失败，该品牌不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }else if (mapper.countByNameAndNotId(updateDTO.getName(), id)>0){
            String message = "更新品牌失败，品牌名称已存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        brand.setId(id);
        BeanUtils.copyProperties(updateDTO,brand);
        int updateRows = mapper.update(brand);
        if (updateRows!=1){
            String message = "更新品牌失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id,0);
    }

    private void updateEnableById(Long id, Integer enable){
        String enableText[] = {"禁用","启用"};

        BrandStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = enableText[enable] + "品牌失败，尝试访问的品牌不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        if (enable == standardVO.getEnable()){
            String message = enableText[enable] + "品牌失败，尝试访问的品牌已被"+enableText[enable];
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(enable);
        int updateRows = mapper.update(brand);
        if (updateRows!=1){
            String message = enableText[enable] + "类别失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        BrandStandardVO standardVO = repository.get(id);
        if (standardVO == null) {
            String message = "查询品牌失败，该品牌不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardVO;
    }

    @Override
    public List<BrandListItemVO> list() {
        return repository.getList();
    }

    /**
     * 重建缓存
     */
    @Override
    public void rebuildCache() {
        //删除所有redis数据
        brandRedisRepository.deleteAll();

        List<BrandListItemVO> list = mapper.list();
        brandRedisRepository.saveList(list);
        log.debug("已将brand数据库的列表数据存入到了redis");

        for (BrandListItemVO brandListItemVO : list){
            Long brandId = brandListItemVO.getId();
            BrandStandardVO brandStandardVO = mapper.getStandardById(brandId);
            brandRedisRepository.save(brandStandardVO);
        }
        log.debug("已将brand数据库的单个数据存入到了redis");
    }
}
